package com.matrix.media.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.videorecog20200320.models.GetAsyncJobResultResponse;
import com.aliyun.videorecog20200320.models.GetAsyncJobResultResponseBody;
import com.matrix.domain.properties.AliProperties;
import com.matrix.media.entity.videorecog.Element;
import com.matrix.media.entity.videorecog.SplitVideoPart;
import com.matrix.media.entity.videorecog.SplitVideoPartsData;
import org.springframework.beans.factory.annotation.Autowired;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.videorecog20200320.models.SplitVideoPartsResponse;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideorecogService {

    /**
     * 视频拆条
     */
    @Autowired
    private AliProperties aliOssProperties;

    public com.aliyun.videorecog20200320.Client createClient() throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId("LTAIgVg8AzB3QrCT")
                .setAccessKeySecret("HGBEW1mRgmwIRzWrhpl1tVYWrhGJ7o");
        // 访问的域名
//        config.endpoint = "oss-cn-shanghai.aliyuncs.com";
        config.endpoint = "videorecog.cn-shanghai.aliyuncs.com";
        return new com.aliyun.videorecog20200320.Client(config);
    }

    public void splitVideoParts(String videoURL) throws Exception {

//        https://my-matrix-media.oss-cn-beijing.aliyuncs.com/da15a13d-4109-41ff-abdc-65cadfddf65a.mp4

        //文件在同地域OSS
        com.aliyun.videorecog20200320.Client client = createClient();
        com.aliyun.videorecog20200320.models.SplitVideoPartsRequest splitVideoPartsRequest = new com.aliyun.videorecog20200320.models.SplitVideoPartsRequest()
                .setVideoUrl(videoURL);


        //文件在本地或文件不在同一地域OSS
        // 场景一，使用本地文件
//         InputStream inputStream = new FileInputStream(new File("/tmp/SplitVideoParts1.mp4"));
        // 场景二，使用任意可访问的url
//        URL url = new URL("https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/videorecog/SplitVideoParts/SplitVideoParts1.mp4");
//        InputStream inputStream = url.openConnection().getInputStream();
//        com.aliyun.videorecog20200320.models.SplitVideoPartsAdvanceRequest splitVideoPartsAdvanceRequest = new com.aliyun.videorecog20200320.models.SplitVideoPartsAdvanceRequest()
//                .setVideoUrlObject(inputStream);


        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();



        try {
            // 复制代码运行请自行打印 API 的返回值
            SplitVideoPartsResponse response = client.splitVideoPartsWithOptions(splitVideoPartsRequest, runtime);
            response.getBody().getRequestId();
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));

            //
            //statusCode
            //body -  requestId

        } catch (TeaException error) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(error));
            // 获取单个字段
            System.out.println(error.getCode());
        }
    }

    public void getAsyncJobResultRequest(String requestId) throws Exception {
        com.aliyun.videorecog20200320.Client client = createClient();
        com.aliyun.videorecog20200320.models.GetAsyncJobResultRequest getAsyncJobResultRequest = new com.aliyun.videorecog20200320.models.GetAsyncJobResultRequest();
        getAsyncJobResultRequest.setJobId(requestId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            GetAsyncJobResultResponse resultResponse = client.getAsyncJobResultWithOptions(getAsyncJobResultRequest, runtime);
            GetAsyncJobResultResponseBody.GetAsyncJobResultResponseBodyData data = resultResponse.getBody().getData();

            String result = data.getResult();
            JSONObject resultObject = JSON.parseObject(result);

            String jobId = data.getJobId();

            String status = data.getStatus();

            System.out.println(resultObject);


            SplitVideoPartsData partsData = new SplitVideoPartsData();
            partsData.setJobId(jobId);

            JSONArray elements = resultObject.getJSONArray("elements" );
            List<Element> elementArr = elements.toJavaList(Element.class);

            partsData.setElements(elementArr);

            JSONArray splitVideoPartResults = resultObject.getJSONArray("splitVideoPartResults");
            List<SplitVideoPart> parts = splitVideoPartResults.toJavaList(SplitVideoPart.class);
            partsData.setSplitVideoParts(parts);


            System.out.println(partsData);


            System.out.println(jobId);


        } catch (TeaException error) {
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}
